package com.suray.wcs.service.resmap;

import com.suray.basic.wcs.path.enums.NodeType;
import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.wcs.service.core.util.Util7z;
import com.suray.wcs.service.system.entity.NodeDB;
import com.suray.wcs.service.system.entity.WareHouseDB;
import com.suray.wcs.service.system.service.impl.NodeDBServiceImpl;
import com.suray.wcs.service.system.service.impl.WareHouseDBServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
@Component
public class WriteMap {
    private static NodeDBServiceImpl nodedbServiceImpl = SpringContextHolder.getBean(NodeDBServiceImpl.class);
    private static WareHouseDBServiceImpl wareHouseDBService = SpringContextHolder.getBean(WareHouseDBServiceImpl.class);

    /**
     * 地图数据写入.map文件
     *
     * @return 目的文件夹
     * @throws IOException
     */
    @PostConstruct
    public static String write() throws IOException {
        List<NodeDB> nodeList = nodedbServiceImpl.getNodeDBList();
        WareHouseDB wareHouseDB = wareHouseDBService.getCurWareHouseDB();
        int mapVersion = wareHouseDB.getMapVersion();
        String name = "ResPlus";
        String paperFile = System.getProperty("user.dir") + File.separator + "wcs_map" + File.separator + wareHouseDB.getWarehouseName() + File.separator;
        //创建文件夹
        String filename = paperFile + name + ".map";
        createPaper(filename);
        File file = new File(filename);
        FileOutputStream fop = new FileOutputStream(file);
        //带小车方向的新版地图
        fop.write(("MapVersion = " + mapVersion).getBytes());
        fop.write("\r\n".getBytes());
        // 不存在的库区默认方向为1
        fop.write(("carDirection = " + (wareHouseDB == null ? 1 : wareHouseDB.getMapDirection())).getBytes());
        fop.write("\r\n".getBytes());
        for (NodeDB gridDB : nodeList) {
            if (!gridDB.getGridType().equals(NodeType.B)) {
                byte[] contentInBytes = transFer(gridDB).getBytes();
                fop.write(contentInBytes);
                fop.write("\r\n".getBytes());
            }
        }
        fop.flush();
        fop.close();
        try {
            Util7z.zip(filename, paperFile + name + ".7z", file.getName());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return filename;
    }


    /**
     * 创建文件夹
     *
     * @param path
     * @throws IOException
     */
    public static void createPaper(String path) throws IOException {
        File file = new File(path);
        File fileParent = file.getParentFile();
        if (!fileParent.exists()) {
            fileParent.mkdirs();
        }
        file.createNewFile();
    }

    /**
     * 数字转换字符串 例如1——>01
     *
     * @param num
     * @return
     */
    public static String intToStr(Integer num) {
        if (num <= 9 && num > 0) {
            return "0" + num;
        } else {
            return "" + num;
        }
    }

    /**
     * 节点信息根据约定格式进行转换
     * 为RES可识别的格式
     *
     * @param nodedb
     * @return
     */
    private static String transFer(NodeDB nodedb) {
        int up = (byte) (nodedb.getCanToUp() == true ? 1 : 0) << 3;
        int down = (byte) (nodedb.getCanToDown() == true ? 1 : 0) << 2;
        int left = (byte) (nodedb.getCanToLeft() == true ? 1 : 0) << 1;
        int right = (byte) (nodedb.getCanToRight() == true ? 1 : 0);
        int type = convert(nodedb.getGridType()) << 4;
        String code = nodedb.getGroundCode();
        if (StringUtils.isEmpty(code)) {
            code = "00000";
        }
        int correctVal = 0x32;
        return Integer.toHexString(nodedb.getGridx()) + "," + Integer.toHexString(nodedb.getGridy()) + ","
                + Integer.toHexString(nodedb.getGridz()) + ","
                + Integer.toHexString(nodedb.getOffsetx()) + "," + Integer.toHexString(nodedb.getOffsety())
                + "," + Integer.toHexString(type + up + down + left + right) + "," + Integer.toHexString(Integer.parseInt(code)) + Integer.toHexString(correctVal)
                + "," + Integer.toHexString(Integer.parseInt(code)) + Integer.toHexString(correctVal) + ",";
    }

    /**
     * 根据类型转换成传输的数字
     *
     * @param ch
     * @return
     */
    private static int convert(NodeType ch) {
        int type;
        switch (ch) {
            case B:
                type = NodeType.B.getCodeNum();
                break;
            case P:
                type = NodeType.P.getCodeNum();
                break;
            case R:
                type = NodeType.R.getCodeNum();
                break;
            case L:
            case TL:
                type = NodeType.L.getCodeNum();
                break;
            case C:
                type = NodeType.C.getCodeNum();
                break;
            case CONNECTION:
                type = NodeType.CONNECTION.getCodeNum();
                break;
            case ST:
                type = NodeType.ST.getCodeNum();
                break;
            case AD:
                type = NodeType.AD.getCodeNum();
                break;
            case AE:
                type = NodeType.AE.getCodeNum();
                break;
            case TE:
                type = NodeType.TE.getCodeNum();
                break;
            default:
                throw new RuntimeException("无法识别的类型数据:" + ch);
        }
        return type;
    }
}
